<%= modules.inject(""){|t,x| "#{t}\nmodule #{x}"} %>
class <%=class_basename%> < ActiveRecord::Base
      attr_accessor :password
      attr_accessor :password_confirmation
      attr_accessor :email_confirmation

      validates_presence_of :username, :email, :email_confirmation,
        :password, :password_confirmation

      validates_length_of :username, :within => 5...100
      validates_length_of :email,
                          :within => 5...100,
                          :unless => Proc.new{|x| x.email.blank?}
      validates_confirmation_of :email
      #email regex lifted from Beast, user.rb
      validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
      validates_uniqueness_of :username, :case_sensitive => false
      validates_length_of :password,
                          :within => 4...40,
                          :unless => Proc.new {|x| x.password.blank?}
      validates_confirmation_of :password

      before_validation :set_username
      before_save :encrypt_password

      # Authenticates a user by their username and unencrypted password.
      # Returns the user or nil if not authenticated or found
      def self.authenticate(login, password)
        u = find_by_username login
        u && u.authenticated?(password) ? u : nil
      end

      def authenticated?(password)
        crypted_password == encrypt(password)
      end

      def encrypt(password)
        self.class.encrypt password, salt
      end

      def self.encrypt(password, salt)
        Digest::SHA1.hexdigest("--#{salt}--#{password}--")
      end

      def remember_token?
        self.remember_token_expires_at && Time.now.utc < self.remember_token_expires_at
    end

    def remember_me
      self.remember_token_expires_at = 2.weeks.from_now.utc
      self.remember_token = encrypt("#{username}--#{remember_token_expires_at}")
      save false
    end

    def forget_me
      self.remember_token_expires_at = nil
      self.remember_token = nil
      save false
    end

    #protected
      # before filter
      def encrypt_password
        return if password.blank?
        self.salt =Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{self.username}") if new_record?
        self.crypted_password = encrypt password
      end

      def set_username
        self.username = self.email
      end
    end
<%= modules.inject(""){|t,x| "#{t}end\n"} %>

